﻿namespace Pub.Class
{
    using System;
    using System.Runtime.InteropServices;

    public class Date
    {
        public class ExactTimeSpan
        {
            private static long _f;
            private static long _tc;

            public static DateTime GetExactNow()
            {
                if (_tc == 0L)
                {
                    long tickCount = GetTickCount();
                    DateTime now = DateTime.Now;
                    _tc = now.Ticks - tickCount;
                    return now;
                }
                return new DateTime(_tc + GetTickCount());
            }

            public static long GetTickCount()
            {
                long lpFrequency = _f;
                switch (lpFrequency)
                {
                    case 0L:
                        if (QueryPerformanceFrequency(ref lpFrequency))
                        {
                            _f = lpFrequency;
                        }
                        else
                        {
                            _f = -1L;
                        }
                        break;

                    case -1L:
                        return (long) (Environment.TickCount * 0x2710);
                }
                long lpPerformanceCount = 0L;
                QueryPerformanceCounter(ref lpPerformanceCount);
                return (long) (((lpPerformanceCount * 1000.0) * 10000.0) / ((double) lpFrequency));
            }

            [DllImport("kernel32.dll")]
            private static extern bool QueryPerformanceCounter([In, Out] ref long lpPerformanceCount);
            [DllImport("kernel32.dll")]
            private static extern bool QueryPerformanceFrequency([In, Out] ref long lpFrequency);
        }
    }
}

